<?php

namespace Controller;

require_once('Model/RegisterHandler.php');
require_once('Validator/Validator.php');

class RegisterController{
		
	public function DoControl(\Model\Database $db){
		
		$registerView = new \View\RegisterView();
		$registerHandler = new \Model\RegisterHandler($db);  
		$validator = new \Validator\Validator();
		$out = "";
		
		$out .= $registerView->RegisterUserBox();
		
		//Har användaren försökt att registrera?
		if($registerView->TriedToRegister()){
			//Är username redan taget?
			if($registerHandler->UserExist($registerView->GetUserName())){
				 $out .=$registerView->UserExistMessage(); 
			} else {
				//Validerar indatat
				if ($validator->ValidateUsername($registerView->GetUserName())==TRUE
				&&($validator->ValidatePassword($registerView->GetPassword(), $registerView->GetPassword2())==TRUE)){
					//Registrera användare	
					$registerHandler->RegisterNewUser($registerView->GetUsername(),$registerView->GetPassword());
					$out .=$registerView->SuccessMessage();	
				}
			}
		}
		$out .=$validator->GetValidationError();
		return $out;
		
	}
	
}

